﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalalCheck.CustomViews;
using HalalCheck.Utils;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Services.Media;

namespace HalalCheck.View
{
    public class SharePage : BaseView
    {
        private IMediaPicker _mediaPicker;
        /// <summary>
        /// The _scheduler.
        /// </summary>
        private readonly TaskScheduler _scheduler = TaskScheduler.FromCurrentSynchronizationContext();

        private Image productPicture;

        public SharePage()
        {
            var layout = new RelativeLayout
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand
            };
            layout.Children.Add(new TileViewHalal(), Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p => p.Bounds.Width)), Constraint.RelativeToParent((p) => p.Bounds.Height));

            productPicture = new Image
            {

            };
            layout.Children.Add(productPicture, Constraint.Constant(0), Constraint.Constant(0),
                Constraint.RelativeToParent((p) => p.Bounds.Width),Constraint.Constant(200));

            var pictureBtn = new Button
            {
                Text = "Take Picture"
            };
            pictureBtn.Clicked += TakePicture;
            layout.Children.Add(pictureBtn, Constraint.Constant(0), Constraint.RelativeToView(productPicture,(p,s)=>s.Bounds.Bottom));


            var shareBtn = new Button
            {
                Text = "Share Product"
            };
            shareBtn.Clicked += ShareProduct;
            layout.Children.Add(shareBtn, Constraint.Constant(0), Constraint.RelativeToView(pictureBtn, (p, s) => s.Bounds.Bottom));
            

            Content = layout;
            _mediaPicker = DependencyService.Get<IMediaPicker>();
        }

        private void ShareProduct(object sender, EventArgs e)
        {
          var sp=  DependencyService.Get<IShareProduct>();
            sp.ShareFB();
            Debug.WriteLine(sp.ToString());
        }

        void TakePicture(object sender, EventArgs e)
        {
            Setup();

            ImageSource pic = null;

            this._mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400,PercentQuality = 20}).ContinueWith(t =>
           {
               if (t.IsFaulted)
               {
                   var s = t.Exception.InnerException.ToString();
               }
               else if (t.IsCanceled)
               {
                   var canceled = true;
               }
               else
               {
                   var mediaFile = t.Result;

                   pic = ImageSource.FromStream(() => mediaFile.Source);
                   productPicture.Source = pic;
                   return mediaFile;
               }

               return null;
           }, _scheduler);
        }

        private void Setup()
        {
            if (_mediaPicker != null)
            {
                return;
            }

            var device = Resolver.Resolve<IDevice>();

            ////RM: hack for working on windows phone? 
            _mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
        }
    }
}
